package com.gt.aep.base.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gt.aep.base.entity.systemManage.SysPermission;
import com.gt.aep.base.entity.systemManage.SysUser;
import com.gt.aep.base.service.systemManage.SysPermissionService;
import com.gt.aep.base.service.systemManage.SysRoleUserService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 获取登陆用户的权限列表
 * @author neko
 */
@Component
public class PermUtil {
    @Autowired
    SysPermissionService sysPermissionService;
    @Autowired
    SysRoleUserService sysRoleUserService;

    public ArrayList<String> userPermission(@Param(value = "permPrefix") String permPrefix) {
        Subject subject = SecurityUtils.getSubject();
        SysUser user=(SysUser)subject.getSession().getAttribute("userKey");

        ArrayList<String> permList = new ArrayList<>();
        QueryWrapper<SysPermission> wrapper = new QueryWrapper<>();
        wrapper.eq("status",1);
        if(permPrefix!=null) {
            System.out.println("perm:不为空");
            wrapper.likeRight("permNo", permPrefix);
        }
        wrapper.orderByAsc("permNo");
        List<SysPermission> list = sysPermissionService.list(wrapper);
        for (SysPermission sysPermission:list) {
            if(subject.isPermitted(sysPermission.getPermNo())) {
                permList.add(sysPermission.getPermNo());
            }
        }


        return permList;
    }

    public  List<Map<String,Object>> getUserPermission() {
        Subject subject = SecurityUtils.getSubject();
        SysUser user=(SysUser)subject.getSession().getAttribute("userKey");
        String Id=user.getId();

        List<Map<String,Object>> sysPermissionList= sysPermissionService.getPermissionByUserId(Id);

        return sysPermissionList;
    }
}
